package com.beilang.hotline.template.page;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.codelookup.model.CodeModel;
import com.beilang.base.core.page.BasePage;
import com.beilang.base.exception.CheckException;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.property.AppDefaultProperty;
import com.beilang.base.util.CodeConstant;
import com.beilang.base.util.DateUtil;
import com.beilang.hotline.template.manager.TemplateMgr;
import com.beilang.hotline.template.model.TemplateModel;
import com.beilang.hotline.template.to.SearchTO;
import com.beilang.hotline.user.model.UserModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("unchecked")
public class TemplatePage extends BasePage{
	private TemplateMgr templateMgr;
	private CodeLookupMgr codeLookupMgr;

	public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
		this.codeLookupMgr = codeLookupMgr;
	}

	public void setTemplateMgr(TemplateMgr templateMgr) {
		this.templateMgr = templateMgr;
	}

	public List getSysList(){
		List list=new ArrayList();
		list.add(this.getEmptyIntCodeModel());
		list.addAll(getAddSysList());
		return list;
	}

	public List getTemplateList(int sysId){
		List list=new ArrayList();
		list.add(this.getEmptyIntCodeModel());
		list.addAll(getSysTemplateList(sysId));
		return list;
	}

    public List getSysTemplateList(int sysId){
        List list=new ArrayList();
        list.add(sysId);
        return codeLookupMgr.getCodeListWithFilter(CodeConstant.TEMPLATE_LIST, CodeConstant.CODE_FILTER, list);
	}

    public List getAddSysList(){
        return codeLookupMgr.getCodeListWithFilter(CodeConstant.SYS_NAME, CodeConstant.CODE_FILTER, new ArrayList());
	}

	public List getUpdateSysList(){
        return this.getAddSysList();
	}

    public PaginationHelper getSearchList(PaginationHelper helper,SearchTO searchTO){
		helper=templateMgr.getTemplateList(helper, searchTO);
		return helper;
	}

	public PaginationHelper deleteTemplateList(PaginationHelper helper,SearchTO searchTO,List deleteList){
        for (Object obj : deleteList) {
            TemplateModel templateModel = (TemplateModel) obj;
            templateMgr.deleteTemplate(templateModel.getTemplateId());
        }
		return this.getSearchList(helper, searchTO);
	}

	public TemplateModel addTemplate(TemplateModel templateModel) throws CheckException{
		Long sysId=templateModel.getSysId();
		List param=new ArrayList();
		param.add(sysId);
		List list=codeLookupMgr.getCodeListWithFilter(CodeConstant.TEMPLATE_LIST, CodeConstant.CODE_FILTER, param);
		if(list!=null&&list.size()>0){
            for (Object obj : list) {
                CodeModel codeModel = (CodeModel) obj;
                if (codeModel.getCodeDesc().equalsIgnoreCase(templateModel.getTemplateName())) {
                    throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_RECORD_ALREADY_EXIST));
                }
            }
		}
		UserModel user=this.getLoginUser();
		templateModel.setCreateId(user.getUserId());
		templateModel.setUpdateId(user.getUserId());
		Date date = DateUtil.getDate();
		templateModel.setCreateTime(date);
		templateModel.setUpdateTime(date);
		templateModel=templateMgr.createTemplate(templateModel);
		return templateModel;
	}

	public TemplateModel getViewTemplate (Long templateId){
		return templateMgr.getTemplate(templateId);
	}

	public TemplateModel getUpdateTemplate (Long templateId){
		return getViewTemplate(templateId);
	}

	public TemplateModel UpdateTemplate(TemplateModel templateModel) throws CheckException{
		Long sysId=templateModel.getSysId();
		List param=new ArrayList();
		param.add(sysId);
		List list=codeLookupMgr.getCodeListWithFilter(CodeConstant.MODULE_LIST, CodeConstant.CODE_FILTER, param);
		if(list!=null&&list.size()>0){
            for (Object obj : list) {
                CodeModel codeModel = (CodeModel) obj;
                if (codeModel.getCodeDesc().equalsIgnoreCase(templateModel.getTemplateName())) {
                    if (!codeModel.getCodeId().equalsIgnoreCase(String.valueOf(templateModel.getTemplateId().longValue())))
                        throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_RECORD_ALREADY_EXIST));
                }
            }
		}
		UserModel user=this.getLoginUser();
		templateModel.setUpdateId(user.getUserId());
		templateModel.setUpdateTime(DateUtil.getDate());
		templateModel=templateMgr.saveTemplate(templateModel);
		return templateModel;
	}
}
